package com.selfdriving.adapter;

import java.util.List;

import com.selfdriving.R;
import com.selfdriving.bean.Feature;
import com.selfdriving.utils.PhotoUtil;
import com.selfdriving.utils.StringUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FeatureListAdapter extends BaseAdapter
{
	private Context context;

	private List<Feature> features;

	private HolderView holderView;

	public FeatureListAdapter(Context context, List<Feature> features)
	{
		this.context = context;
		this.features = features;
	}

	@Override
	public int getCount()
	{
		return features.size();
	}

	@Override
	public Object getItem(int poistion)
	{
		return features.get(poistion);
	}

	@Override
	public long getItemId(int poistion)
	{
		return poistion;
	}

	@Override
	public View getView(int poistion, View view, ViewGroup arg2)
	{
		Feature feature = features.get(poistion);
		if (view == null)
		{
			holderView = new HolderView();
			view = ((LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.popular_list_item, null);
			view.setTag(holderView);
		} else
		{
			holderView = (HolderView) view.getTag();
		}
		holderView.name = (TextView) view.findViewById(R.id.tvName);
		holderView.num = (TextView) view.findViewById(R.id.tvNum);
		holderView.icon = (ImageView) view.findViewById(R.id.ivPosition);
		holderView.postion = (TextView) view.findViewById(R.id.tvPosition);

		if (!StringUtil.isNullOrEmpty(feature.name))
		{
			holderView.name.setText(feature.name);
		}

		if (!StringUtil.isNullOrEmpty(feature.popularNum))
		{
			holderView.num.setText(feature.popularNum);
		}

		if (poistion == 0)
		{
			holderView.icon.setImageBitmap(PhotoUtil.readBitmap(context,
					R.drawable.notification_1));
			holderView.postion.setVisibility(View.GONE);
		} else if (poistion == 1)
		{
			holderView.icon.setImageBitmap(PhotoUtil.readBitmap(context,
					R.drawable.notification_2));
			holderView.postion.setVisibility(View.GONE);
		} else if (poistion == 2)
		{
			holderView.icon.setImageBitmap(PhotoUtil.readBitmap(context,
					R.drawable.notification_3));
			holderView.postion.setVisibility(View.GONE);
		} else if (poistion == 3)
		{
			holderView.icon.setImageBitmap(PhotoUtil.readBitmap(context,
					R.drawable.notification_4));
			holderView.postion.setVisibility(View.GONE);
		} else
		{
			holderView.postion.setVisibility(View.VISIBLE);
			holderView.postion.setText(poistion + 1 + "");
			holderView.icon.setVisibility(View.INVISIBLE);
		}

		return view;
	}

	private class HolderView
	{
		private ImageView icon;

		private TextView name;

		private TextView num;

		private TextView postion;
	}

}
